exports.install = function (Vue, options) {

    Vue.prototype.initLayui = function initLayui() {
        var that = this;
        $('body').on('mouseover', '*[lay-tips]', function (obj) {
            let a = this;
            layui.use("layer", function () {
                var layer = layui.layer;
                var index = layer.tips($(a).attr("lay-tips"), a);
                $(a).attr("tips-index", index);
            })
        });
        $('body').on('mouseout', '*[lay-tips]', function (obj) {
            let a = this;
            layui.use("layer", function () {
                var layer = layui.layer;
                layer.close($(a).attr("tips-index"));
            })
        });
        //初始化日期文本框
        $("input[laydate]").each(function () {
            var id = that.getID();
            $(this).attr("id", id);
            var type = $(this).attr("laydate");
            var format = $(this).attr("format");
            layui.use("laydate", function () {
                var laydate = layui.laydate;
                laydate.render({
                    elem: "#" + id,
                    type: type,
                    format: format,
                })
            })
        });
        //初始select框默认选中
        $("select[lay-select]").each(function () {
            var selectValue = $(this).attr("lay-select");
            $(this).find("option[value='" + selectValue + "']").attr("selected", true);
        });
        //初始select框默认选中
        $("input[lay-checkbox]").each(function () {
            var selectValue = $(this).attr("lay-checkbox");
            if (selectValue == "true") {
                $(this).attr("checked", "checked");
            }
        });
        //设置layui from label换行问题
        $(".layui-form-label").css("white-space", "nowrap");
        layui.use(['form', 'element', 'laydate'], function () {
            var form = layui.form;
            var element = layui.element;
            var laydate = layui.laydate;
            form.render();
            element.render();
            laydate.render();
            form.on('select(change)', function (obj) {
                var name = $(obj.elem).attr("lay-event");
                let a = obj.elem;
                let data = obj.value;
                if (!that.checkIsNull(name)) {
                    that[name](data, a);
                }
            });
            form.on('switch(change)', function (obj) {
                var name = $(obj.elem).attr("lay-event");
                let data = {};
                let a = obj.elem;
                data.value = obj.value;
                data.checked = this.checked;
                if (!that.checkIsNull(name)) {
                    that[name](data, a);
                }
            });
            form.on('checkbox(change)', function (obj) {
                var name = $(obj.elem).attr("lay-event");
                let data = {};
                let a = obj.elem;
                data.value = obj.value;
                data.checked = obj.elem.checked;
                if (!that.checkIsNull(name)) {
                    that[name](data, a);
                }
            });
        })
    }

    Vue.prototype.openConfirm = function openConfirm(content, yes, cancel) {
        var that = this;
        layui.use("layer", function () {
            var layer = layui.layer;
            layer.confirm(content, {
                icon: 3,
                title: "温馨提示",
            }, yes, cancel);
        })
    }

    Vue.prototype.openLoading = function openLoading(obj, callBack) {
        var that = this;
        if (typeof (obj) == "function") {
            callBack = obj;
            obj = "";
        }
        var index;
        layui.use('layer', function () {
            var layer = layui.layer;
            if (obj == null || obj == "" || typeof (obj) == "undefined") {
                index = layer.load();
            } else {
                var icon = obj.icon;
                if (icon == null || icon == "" || typeof (icon) == "undefined") {
                    icon = 0;
                }
                index = layer.load(icon, obj);
            }
            if (!that.checkIsNull(callBack)) {
                callBack(index);
            }
        })
    }

    Vue.prototype.closeParentOpen = function closeOpen(index) {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    Vue.prototype.closeOpen = function closeOpen(index) {
        var that = this;
        layui.use('layer', function () {
            var layer = layui.layer;
            if (!that.checkIsNull(index)) {
                layer.close(index);
            } else {
                layer.closeAll();
            }

        })
    }

    Vue.prototype.closeLoading = function closeLoading() {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.closeAll('loading');
        })
    }

    Vue.prototype.closeAllOpen = function closeAllOpen() {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.closeAll();
        })
    }

    Vue.prototype.openMsg = function openMsg(msg, isError) {
        var icon = 6;
        if (!isError) {
            icon = 5;
        }
        var obj = {
            type: 0,
            title: "温馨提示",
            content: msg,
            offset: 'rb',
            icon: icon,
            anim: 2,
        }
        top.layui.layer.open(obj);
    }

    Vue.prototype.showMsg = function showMsg(text, isOk) {
        var icon = 6;
        if (!isOk) {
            icon = 5;
        }
        layui.use('layer', function () {
            layer.msg(text, {
                icon: icon,
                anim: 6,
            });
        })
    }

    Vue.prototype.renderTable = function renderTable(obj) {
        var that = this;
        if (this.checkIsNull(obj)) {
            throw new Error("请求参数必传项");
        }
        if (this.checkIsNull(obj.url)) {
            throw new Error("请求参数url必传项");
        }
        if (this.checkIsNull(obj.tableId)) {
            throw new Error("请求参数tableId必传项");
        }
        if (this.checkIsNull(obj.cols)) {
            throw new Error("请求参数cols必传项");
        }
        var tableId = obj.tableId;
        if (!this.checkIsNull(obj.limits)) {
            limits = obj.limits;
        }
        var filter = this.getID();
        obj.elem = "#" + tableId;
        obj.limits = this.checkIsNull(obj.limits) ? [10, 20, 30, 40, 50, 60, 100] : obj.limits;
        obj.method = this.checkIsNull(obj.method) ? 'post' : obj.method;
        obj.layout = this.checkIsNull(obj.layout) ? ["prev", "page", "next", "skip", "count", "limit"] : obj.layout;
        obj.page = this.checkIsNull(obj.page) ? true : obj.page;
        obj.loading = this.checkIsNull(obj.loading) ? true : obj.loading;
        obj.request = this.checkIsNull(obj.request) ? {
            pageName: "pageNum",
            limitName: "pageSize",
        } : obj.request;
        obj.parseData = this.checkIsNull(obj.parseData) ? function (res) {
            var newData = {};
            if (res.code == "000000") {
                newData.code = 0;
                if (that.checkIsNull(res.result)) {
                    newData.data = [];
                    newData.count = 0;
                } else {
                    newData.data = res.result.list;
                    newData.count = res.result.total;
                }
            } else {
                newData.code = 1;
                newData.msg = res.msg;
                newData.data = [];
                newData.count = 0;
            }
            return newData;
        } : obj.parseData;
        //设置lay-filter
        $(obj.elem).attr("lay-filter", filter);
        layui.use("table", function () {
            var table = layui.table;
            table.render(obj);
            table.on('tool(' + filter + ')', function (obj) {
                var name = obj.event;
                let data = obj.data
                let a = this;
                that[name](data, a);
            });
        })
    }

    Vue.prototype.open = function open(obj, callBack) {
        var that = this;
        layui.use('layer', function () {
            var layer = layui.layer;
            obj.success = that.checkIsNull(obj.success) ? obj.success = function (layero, index) {
                if (typeof (callBack) === "function") {
                    callBack(index);
                }
            } : obj.success;
            layer.open(obj);
        })
    }

    Vue.prototype.openContent = function openContent(obj, callBack) {
        var that = this;
        obj.type = 1;
        obj.area = this.checkIsNull(obj.area) ? ["80%", "80%"] : obj.area;
        obj.anim = this.checkIsNull(obj.anim) ? Math.ceil(Math.random() * 6) : obj.anim;
        obj.resize = this.checkIsNull(obj.resize) ? false : obj.resize;
        obj.success = function (layero, index) {
            layer.setTop(layero);
            if (!that.checkIsNull(callBack) && typeof (callBack) == "function") {
                callBack(index);
            }
        }
        this.open(obj);

    }

    Vue.prototype.openPage = function openPage(obj, callBack) {
        var that = this;
        if (this.checkIsNull(obj)) {
            throw new Error("请求参数必传项");
        }
        if (this.checkIsNull(obj.url)) {
            throw new Error("请求参数url必传项");
        }
        var url = obj.url;
        obj.title = this.checkIsNull(obj.title) ? "编辑" : obj.title;
        obj.anim = this.checkIsNull(obj.anim) ? Math.ceil(Math.random() * 6) : obj.anim;
        obj.area = this.checkIsNull(obj.area) ? ["80%", "80%"] : obj.area;
        obj.resize = this.checkIsNull(obj.resize) ? false : obj.resize;
        obj.content = url;
        obj.type = 2;
        obj.success = function (layero, index) {
            layer.setTop(layero);
            if (!that.checkIsNull(callBack) && typeof (callBack) == "function") {
                callBack(index);
            }


        }
        this.open(obj);
    }

    Vue.prototype.selectTree = function selectTree(obj) {
        var tree = obj.tree;
        if (this.checkIsNull(tree)) {
            throw new Error("请求参数tree必传项,参数详情参考layui文档tree组件");
        }
        tree.showCheckbox = false;
        tree.edit = false;
        tree.accordion = true;
        tree.onlyIconControl = true;
        tree.isJump = false;
        tree.showLine = true;
        tree.text = {
            defaultNodeName: '未命名', //节点默认名称
            none: '无数据' //数据为空时的提示文本
        }
        layui.use("tree", function () {
            var tree = layui.tree;
            tree.render(obj.tree);
        })

    }

    //递归循环layui树形数据
    Vue.prototype.recursionLayuiTreeData = function recursionLayuiTreeData(obj) {
        var data = obj.data;
        var title = obj.title;
        var value = obj.value;
        var children = obj.children;
        var spread = obj.spread;
        var checked = obj.checked;
        var disabled = obj.disabled;
        if (this.checkIsNull(data)) {
            throw new Error("请求参数data必传项");
        }
        if (this.checkIsNull(title)) {
            throw new Error("请求参数title必传项,tree显示的节点名称属性");
        }
        if (this.checkIsNull(value)) {
            throw new Error("请求参数value必传项,tree显示的节点id属性");
        }
        if (this.checkIsNull(children)) {
            throw new Error("请求参数children必传项,tree显示的节点children属性");
        }
        if (this.checkIsNull(spread)) {
            spread = [];
        }
        if (this.checkIsNull(checked)) {
            checked = [];
        }
        if (this.checkIsNull(disabled)) {
            disabled = [];
        }
        if (!spread instanceof Array) {
            throw new Error("请求参数spread必须为数组或者集合，传递项为Id值");
        }
        if (!checked instanceof Array) {
            throw new Error("请求参数checked必须为数组或者集合，传递项为Id值");
        }
        if (!disabled instanceof Array) {
            throw new Error("请求参数disabled必须为数组或者集合，传递项为Id值");
        }
        var list = [];
        for (var i = 0; i < data.length; i++) {
            var obj = {};
            var titleData = data[i][title];
            var id = data[i][value];
            var childrenData = data[i][children];
            var childrenList = [];
            if (!this.checkIsNull(childrenData) && childrenData.length > 0) {
                var ch = {};
                ch.data = childrenData;
                ch.title = title;
                ch.value = value;
                ch.children = children;
                childrenList = this.recursionLayuiTreeData(ch);
            }
            var treeData = {};
            treeData.name = titleData;
            treeData.id = id;
            //设置展开Id信息
            for (var j = 0; j < spread.length; j++) {
                if (id == spread[j]) {
                    treeData.spread = true;
                }
            }
            //设置选中Id信息
            for (var j = 0; j < checked.length; j++) {
                if (id == checked[j]) {
                    treeData.checked = true;
                }
            }
            //设置禁用Id信息
            for (var j = 0; j < disabled.length; j++) {
                if (id == disabled[j]) {
                    treeData.disabled = true;
                }
            }
            treeData.children = childrenList;
            list.push(treeData);
        }
        return list;
    }

    Vue.prototype.layuiCode = function layuiCode(id, encode) {
        layui.use("code", function () {
            layui.code({
                elem: "#" + id,
                about: false,
                skin: "notepad",
                encode: encode,
            });
        });
    }

    Vue.prototype.transfer = function transfer(obj) {
        layui.use("transfer", function () {
            var transfer = layui.transfer;
            if (obj.isReload == true) {
                transfer.reload(obj);
            } else {
                transfer.render(obj);
            }
        })
    }

    Vue.prototype.echart = function (obj) {
        var that = this;
        if (this.checkIsNull(obj)) {
            throw new Error("图表信息为必传信息");
        }
        if (this.checkIsNull(obj.title)) {
            throw new Error("图标标题为必传");
        }
        if (this.checkIsNull(obj.elem) || !obj.elem instanceof Element) {
            throw new Error("elem必须为dom对象");
        }
        if (this.checkIsNull(obj.url)) {
            throw new Error("请求url地址必传信息");
        }
        // 设置默认图表显示为饼状图
        if (this.checkIsNull(obj.series) || !obj.series instanceof Array) {
            throw new Error("图表series为必传且必须为集合");
        }
        // 默认显示弹框组件
        if (this.checkIsNull(obj.tooltip.show)) {
            obj.tooltip.show = true;
        }
        // 默认显示图列组件为左边
        if (this.checkIsNull(obj.legend)) {
            obj.legend = {
                type: "scroll",
                left: "left",
                top: 20,
                bottom: 20,
            }
        }
        // 根据图标类型获取
        if (this.checkIsNull(obj.xAxis) || this.checkIsNull(obj.yAxis)) {
            obj.tooltip.trigger = "item";
        } else {
            obj.tooltip.trigger = "false";
        }
        this.post(url, obj.data, function (data) {
            if (that.checkIsNull(obj.callBack)) {
                obj.series[0].data = data;
            } else {
                obj.series[0].data = obj.callBack(data);
            }
            if (!obj.series[0].data instanceof Array) {
                throw new Error("图表series.data必须为集合");
            }
            // 进行处理处理
            if (!that.checkIsNull(obj.parseData) && typeof (obj.parseData) == "function") {
                for (var i = 0; i < obj.series[0].data.length; i++) {
                    var o = obj.series[0].data[i];
                    o = obj.parseData(o);
                    obj.series[0].data[i] = o;
                }
            }
            // 开始渲染图表信息
            layui.use("echarts", function () {
                var echarts = layui.echarts;
                var charts = echarts.init(obj.elem);
                charts.setOption(obj);
            })
        })
    }
}